﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MediaRecommender.Model;
using MediaRecommender.Controller;

namespace MediaRecommender
{
    public partial class MediaSelect : System.Web.UI.Page
    {
        protected string tryOut = "";

        private string emptyCover = "http://thumbs.dreamstime.com/m/book-empty-cover-white-28912004.jpg";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MediaSelectList.Items.Add("Media");
                MediaSelectList.Items.Add("Video Game");
                MediaSelectList.Items.Add("Movie");
                MediaSelectList.Items.Add("Book");
            }
            if (Request.QueryString.AllKeys.Count(x => true) != 0)
            {
                string mediaSelected = Request.QueryString.AllKeys[0].Replace(" ", "");
                string request = Request.QueryString.AllKeys[1];

                if (!IsPostBack)
                {
                    SearchBox.Text = request;
                    MediaSelectList.SelectedIndex = Convert.ToInt32(Request.QueryString[mediaSelected]);
                }

                List<Media> mediaList = MediaCollection.Instance.GetMedia(x => x.Title.ToLower().Contains(request.ToLower()));

                int maxShowing = 16;
                int displayCount = 0;
                foreach (Media m in mediaList)
                {
                    string mediaToDisplay = m.GetType().ToString();
                    if (mediaToDisplay.Contains(mediaSelected))
                    {
                        //construct table cell
                        ImageButton imgButton = new ImageButton();
                        TextBox textbox = new TextBox();
                        TableCell td = new TableCell();

                        imgButton.Attributes["style"] = "float:buttom";
                        textbox.Attributes["style"] = "position:relative";
                        textbox.Attributes["style"] = "float:top";
                        textbox.Width = 214;
                        textbox.Attributes["style"] = "background-color:#e0ffff";

                        textbox.ReadOnly = true;
                        if (m.CoverUrl.Length != 0 && m.CoverUrl != null)
                        {
                            imgButton.ImageUrl = m.CoverUrl;
                        }
                        else imgButton.ImageUrl = emptyCover;

                        textbox.Text = m.Title; td.Text = Convert.ToString(m.ID);

                        if (m is VideoGame)
                        {
                            VideoGame vg = (VideoGame)m;
                            foreach (int p in vg.Platforms)
                            {
                                textbox.Text += "(" + Enum.GetName(typeof(Platform), p) + ")";
                            }
                        }
                        imgButton.Height = 350;
                        imgButton.Width = 214;
                        imgButton.Command += Image_Click;
                        imgButton.ToolTip = showInfo(m);
                        td.Controls.Add(textbox);
                        td.Controls.Add(imgButton);
                        //

                        //determain wether or not a new line is needed and then adds it and the cell or just the cell
                        TableRowCollection trList = TableHeader.Rows;
                        int count = trList.Count; //needed to acces the last row

                        if (trList[count - 1].Cells.Count > 3)
                        {
                            TableRow tr = new TableRow();
                            tr.Cells.Add(td);
                            TableHeader.Rows.Add(tr);
                        }
                        else
                        {
                            trList[count - 1].Cells.Add(td);
                        }
                        displayCount++;
                        if (maxShowing <= displayCount) break;
                    }
                }
            }
        }

        private string showInfo(Media m) {
            string infoToReturn = "";

            infoToReturn += "Genre: ";
            foreach (Genre g in m.Genres)
            {
                infoToReturn += Enum.GetName(typeof(Genre), g) + ", ";
            }

            if (m is VideoGame)
            {
                VideoGame VG = (VideoGame)m;
                infoToReturn += "\nPlatforms: ";
                foreach (Platform p in VG.Platforms)
                    infoToReturn += Enum.GetName(typeof(Platform), p) + ", ";
                infoToReturn += "\nPublisher: " + VG.Publisher.Name;
                infoToReturn += "\nStudio: " + VG.Studio.Name;
            }
            else if (m is Movie)
            {
                Movie MO = (Movie)m;
                int maxActors = 5;
                int count = 0;
                infoToReturn += "\nActors: ";
                foreach (Actor a in MO.Actors)
                {
                    infoToReturn += a.Name + ", ";
                    count++;
                    if (maxActors < count) break;
                }
                infoToReturn += "\nDirector: " + MO.Director.Name;
            }
            else
            {
                Book B = (Book)m;
            }
            return infoToReturn;
        }

        protected void Image_Click(object sender, EventArgs e)
        {
            ImageButton img = (ImageButton)sender;
            TableCell td = (TableCell)img.Parent;
            MedialistController mlc = new MedialistController(UserCollection.Instance.GetUser((string)Session["Username"]));

            string alertMsg = "";
            foreach (Media m in MediaCollection.Instance.GetMedia(x => x.ID == Convert.ToUInt32(td.Text)))
            {
                if (RatingList.SelectedItem != null)
                {
                    mlc.AddMedia(m, Convert.ToInt32(RatingList.SelectedItem.Text));
                    alertMsg = m.Title + " has been added to your medialist!";
                }
                else alertMsg = "You need to choose a rating before adding a media!";

                ScriptManager.RegisterStartupScript(this, this.GetType(), m.Title, "alert('" + alertMsg + "');", true);
            }
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("MediaSelect.aspx" + "?" + MediaSelectList.SelectedItem.Text + "=" + MediaSelectList.SelectedIndex + "&" + SearchBox.Text + "=");
        }
    }
}